package cn.findviewbydream.evil.main.controller;

import cn.findviewbydream.evil.main.service.ReleaseNewpostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/********************************************
 * 工程名：HebeuCS
 * 包名：cn.findviewbydream.evil.main.controller
 * 时间：16-11-10
 * 作者：白雨濃 evil
 * 邮箱：evilbai@foxmail.com
 * 内容：贴子发布入口 {@link CodeController 的子标签}
 ********************************************/
@Controller
public class ReleaseNewpostController
{
    private final ReleaseNewpostService releaseNewpostService;

    @Autowired
    public ReleaseNewpostController(ReleaseNewpostService releaseNewpostService)
    {
        this.releaseNewpostService = releaseNewpostService;
    }


    /**
     * 发布新贴入口
     *
     * @param session 获取用户 判断是否登录
     * @param model   model
     * @return jsp/login
     */
    @RequestMapping("/code/release/newpost")
    public String releaseNewPost(HttpSession session, Model model)
    {
        model.addAttribute("post", null);
        model.addAttribute("codeTypeList", releaseNewpostService.getCodeTypeList());
        return session.getAttribute("user") != null ?
                "code/release_newpost" : "login";
    }


    /**
     * 修改旧贴入口
     *
     * @param session 获取用户 判断是否登录
     * @param post_id 文章id
     * @param model   model
     * @return jsp/login
     */
    @RequestMapping("/code/release/{post_id}")
    public String releaseNewPost(HttpSession session, @PathVariable("post_id") String post_id, Model model)
    {
        model.addAttribute("post", post_id);
        model.addAttribute("codeTypeList", releaseNewpostService.getCodeTypeList());
        return session.getAttribute("user") != null ?
                "code/release_newpost" : "login";
    }


    /**
     * 提交发布
     *
     * @param session  获取用户信息
     * @param title    标题
     * @param codetype 代码类型
     * @param tags     标签（多个）
     * @param md       markdown文本内容
     * @param html     html文本内容
     * @return 状态信息
     */
    @RequestMapping(value = "/code/release/submitrelase",
            method = RequestMethod.POST,
            produces = {"text/html;charset=UTF-8"})
    @ResponseBody
    public String submitRelase(HttpSession session,
                               String title,
                               String codetype,
                               String tags,
                               String md,
                               String html)
    {
        return releaseNewpostService.submitRelase(session,
                title,
                codetype,
                tags,
                md,
                html).toString();
    }

    /**
     * 通过post_id获取markdown的内容
     *
     * @return markdown
     */
    @RequestMapping(value = "/code/release/getmd",
            produces = {"text/html;charset=UTF-8"})
    @ResponseBody
    public String getMarkDown(int post_id)
    {

        return "";
    }

}
